package ckw.gui.wizards;

import java.util.ArrayList;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import ckw.database.OKW;

/**
 * @author DoomThrower
 *
 */
public class WzdConfigurationPg2 extends WizardPage {
	private ArrayList<ConfigurationInputGUI> selectedOKWsGUI; /*
															 * lista formularzy,
															 * generowana na
															 * podstawie wyboru
															 * z pierwszej
															 * strony wizarda
															 */

	public ArrayList<ConfigurationInputGUI> getSelectedOKWsGUI() {
		return selectedOKWsGUI;
	}

	private ScrolledComposite scrolledContainer;
	private Composite container;

	public WzdConfigurationPg2() {
		super("Krok 2.");
		setTitle("Krok 2.");
		setDescription("Prosze wprowadzic wszystkie dane konfiguracyjne wybranych Okregowych Komisji Wyborczych");

		selectedOKWsGUI = new ArrayList<ConfigurationInputGUI>();
	}

	public void createControl(Composite parent) {
		scrolledContainer = new ScrolledComposite(parent, SWT.V_SCROLL
				| SWT.BORDER);
		// Required to avoid an error in the system
		setControl(scrolledContainer);
	}

	// Metoda wywolywana, do zapelnienia formularzy konfiguracyjnych w kroku
	// drugim wizarda
	public void onEnterPage(int[] pageOneSelectionIndices) {
		//
		if(container != null) {
			container.dispose();
		}
		container = new Composite(scrolledContainer, SWT.NONE);
		scrolledContainer.setContent(container);

		GridLayout gridLayout = new GridLayout();
		container.setLayout(gridLayout);
		
		ArrayList<OKW> okwList = ((WzdConfiguration) getWizard())
				.getOkwList();
		for (int i = 0; i < selectedOKWsGUI.size(); i++) {
			selectedOKWsGUI.get(0).getGroup().dispose();
			selectedOKWsGUI.remove(0);
		}
		for (int i = 0; i < pageOneSelectionIndices.length; i++) {
			selectedOKWsGUI.add(new ConfigurationInputGUI(container, okwList
					.get(pageOneSelectionIndices[i])));
		}
		container.pack();

	}

	public boolean canFinish() {
		boolean canFinish = true;
		for (int i = 0; i < selectedOKWsGUI.size() && canFinish; i++) {
			ConfigurationInputGUI ciGUI = selectedOKWsGUI.get(i);
			if (ciGUI.getCipherKeyText().getText() == ""
					|| ciGUI.getIpText().getText() == ""
					|| ciGUI.getPasswordText().getText() == "") {
				canFinish = false;
			}
		}
		return canFinish;
	}
}
